import React, {Component} from 'react'
import { Map, is } from 'immutable';
import {ListComponent} from '../../../components';
import { Form } from 'antd';
import OtherInfoForm from './OtherInfoForm';
const WrappedForm = Form.create()(OtherInfoForm);

export default class OtherInfo extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            rowObj: {},
            isAdd: true
        };
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'rowClick',
                showName: '行单击选择事件'
            }
        ];
        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏（用于列配置显示隐藏）, foreverHidden: true 列永久隐藏
        this.tableColumn = {
            data: [
                {
                    showName: 'Id',
                    columnName: 'custOtherId',
                    columnHidden:true,
                    foreverHidden: true
                },{
                    showName: '客户编码',
                    columnName: 'custCode',
                    columnWidth: 130
                },{
                    showName: '类型',
                    columnName: 'typeName'
                },{
                    showName: '入库验证',
                    columnName: 'inSeriesVerify',
                    showOption:[//页面显示配置
                        {
                            optionKey:'1',
                            optionShow:'是'
                        },{
                            optionKey:'0',
                            optionShow:'否'
                        }
                    ]
                },{
                    showName: '出库验证',
                    columnName: 'outSeriesVerify',
                    showOption:[//页面显示配置
                        {
                            optionKey:'1',
                            optionShow:'是'
                        },{
                            optionKey:'0',
                            optionShow:'否'
                        }
                    ]
                },{
                    showName: '生效期',
                    columnName: 'effectiveDate'
                },{
                    showName: '失效期',
                    columnName: 'expirationDate'
                },{
                    showName: '允许超量收货',
                    columnName: 'allowExcessReceipt',
                    showOption:[//页面显示配置
                        {
                            optionKey:'1',
                            optionShow:'是'
                        },{
                            optionKey:'0',
                            optionShow:'否'
                        }
                    ]
                },{
                    showName: '允许超量百分比',
                    columnName: 'excessReceiptPercent',
                },{
                    showName: '创建人',
                    columnName: 'createBy'
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreateStr'
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy'
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified'
                }
            ]
        };
    }
    rowHandles = (strHandles, rowObj) => {
        if(strHandles == 'rowClick'){
            this.rowClickHandle(rowObj);
        }
    }

    rowClickHandle = (rowObj) => {
        if(rowObj.length === 0){
            this.setState({ rowObj: {}, isAdd: true });
        }else{
            let blCompare = false;  //比较state中rowObj是否变化，如果没有变化不需要setState
            const map1 = Map(rowObj[0].data);
            const map2 = Map(this.state.rowObj);
            if(!is(map1, map2)){
                blCompare = true;
            }
            if(blCompare){
                this.setState({ rowObj: rowObj[0].data, matchedRows: [rowObj[0].rowIndex], isAdd: false });
            }
        }
    }
    refreshHandle = () => {
        this.refs.listComponent.refreshHandle();
    }

    render() {
        return (
            <div>
                <ListComponent
                    ref = "listComponent"
                    url = {this.props.url}
                    searchCondition={this.props.searchCondition}
                    tableColumnData = {this.tableColumn}
                    rowOperation = {this.rowOperation}
                    rowHandles = {this.rowHandles}
                    isRowSingle = "true"
                    subtractHeight = '0'
                    subtractWidth = '145'
                    matchedRows = {this.state.matchedRows}
                />

                <WrappedForm
                    refreshHandle = {this.refreshHandle}
                    searchCondition={this.props.searchCondition}
                    rowObject={this.state.rowObj}
                    isAdd={this.state.isAdd}
                />
            </div>
        );
    }
}
